package com.zcy.datasource;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;

/**
 * Created by join on 2017/7/3.
 */

@Configuration
@PropertySource("classpath:/application.properties")
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.zcy"})
public class DataSourceConfig {

    private static InternalLogger logger = InternalLoggerFactory.getInstance(DataSourceConfig.class);

    @Resource(name="entityManagerFactory")
    private EntityManagerFactory entityManagerFactory;

    /**
     * 描述 : 负责解析资源文件
     * 这个类必须有，而且必须声明为static，否则不能正常解析
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() {
        logger.info("PropertySourcesPlaceholderConfigurer");
        return new PropertySourcesPlaceholderConfigurer();
    }

//    @Bean(name="entityManagerFactory")
//    public EntityManagerFactory entityManagerFactory() {
//        return entityManagerFactory;
//    }

    @Bean(name="transactionManager")
    public DsJpaTransactionManager transactionManager() {
        DsJpaTransactionManager transactionManager = new DsJpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
    }

}
